#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef pair<int, int> PII;
#define x first
#define y second
const int N = 210;
char g[N][N];
PII st, ed;
int d[N][N];
int n, m;
int dx[] = { -1,0,1,0 }, dy[] = { 0,1,0,-1 };
void bfs()
{
	queue<PII>q;
	q.push(st);
	d[st.x][st.y] = 0;
	while (q.size()) {
		PII t = q.front();
		q.pop();
		for (int i = 0; i < 4; i++) {
			int a = t.x + dx[i];
			int b = t.y + dy[i];
			if (g[a][b] == '#' || d[a][b] != -1)continue;
			if (a <= 0 || a > n || b <= 0 || b > m)continue;
			d[a][b] = d[t.x][t.y] + 1;
			if (a == ed.x && b == ed.y)return;
			q.push({ a,b });
		}
	}
	return;
}
int main()
{
	int t;
	cin >> t;
	while (t--) {
		memset(g, ' ', sizeof(g));
		memset(d, -1, sizeof(d));
		cin >> n >> m;
		for (int i = 1; i <= n; i++) {
			for (int j = 1; j <= m; j++) {
				cin >> g[i][j];
				if (g[i][j] == 'S') {
					st = { i,j };
				}
				if (g[i][j] == 'E') {
					ed = { i,j };
				}
			}
		}
		bfs();
		if (d[ed.x][ed.y] == -1)  cout << "oop!" << endl;
		else cout << d[ed.x][ed.y] << endl;
	}
	return 0;
}